<<style>

</style>

<template>
    <div class="table">
        <!-- 头部菜单按钮 -->
        <div class="table-header">
             <Row>
                <Col span="12">
                    <Button type="success" icon="android-add" @click="showAdd = true">添加</Button>
                </Col>
                <Col span="12">
                    <div style="float:right;"> 
                        <Input v-model="keywords" placeholder="请输入..." style="width: 150px"></Input>
                        <Button type="ghost" icon="ios-search">搜索</Button>
                    </div>
                </Col>
            </Row>
        </div>
        <can-edit-table refs="table" :hover-show="true" :edit-incell="true" :columns-list="columns" :table-data="list" :saveEditIncell="saveEditIncell"></can-edit-table>
        <!-- <Table border :columns="columns" :data="list"></Table> -->
        <Page :current.sync="page" :total="total_count" :page-size="limit" @on-change="getList"  show-total show-elevator :style="{'text-align': 'center'}"></Page>

        <Modal v-model="showAdd" title="添加" width="600px" :mask-closable="false" :styles="{top: '80px'}">
	        <add @addcancel="closeAdd" ></add>
            <div slot="footer"></div>
	    </Modal>
        <Modal v-model="showEdit" title="编辑课程分类" width="600px" :mask-closable="false" :style="{top: '80px'}">
            <edit ref="EditModal" @editcancle="closeEdit"></edit>
            <div slot="footer"></div>
	    </Modal>    
    </div>
</template>
<<script>
import http from '../../../libs/http';
import canEditTable from '../../components/canEditTable.vue';
import add from './add';
import edit from './edit';
export default {
    components: {
        canEditTable,
        add,
        edit
    },
    data () {
        return {
            showAdd: false,
            showEdit: false,
            page: 1,
            limit: 15,
            total_count: 0,
            keywords: '',     
            list: [],
            columns: [
                {
                    title: 'ID',
                    key: 'id',
                    align: 'center'
                },
                {
                    title: '名称',
                    key: 'name',
                    align: 'center'
                },
                {
                    title: '类型',
                    key: 'type',
                    align: 'center',
                    render: (h, params) => {
                        switch (params.row.type) {
                            case 1:
                                return '视频';
                                break;
                            case 2:
                                return '文章';
                                break;
                            case 3: 
                                return '直播';
                                break;
                        }
                    }
                },
                {
                    title: '排序值',
                    key: 'listorder',
                    align: 'center',
                    editable: true                    
                },
                {
                    title: '创建时间',
                    key: 'create_time',
                    align: 'center'
                },
                {
                    title: '修改时间',
                    key: 'update_time',
                    align: 'center'
                },
                {
                    title: '操作',
                    key: 'action',
                    align: 'center',
                    render: (h, params) => {
                        return h('div', [
                            h('Button',{
                                props: {
                                    type: 'primary',
                                    size: 'small'
                                },
                                style: {
                                    marginRight: '10px'
                                },
                                on: {
                                    click: () => {
                                        this.edit(params.row.id)
                                    }
                                }
                            }, '编辑'),
                            h('Button', {
                                props: {
                                    type: 'error',
                                    size: 'small'
                                },
                                on: {
                                    click: () => {
                                        this.remove(params.row.id)
                                    }
                                }
                            }, '删除')
                        ])
                    }
                }
            ],
        }
    },
    methods: {
        getList(page) {
            this.page = page;
            var that = this;
            var url = 'admin/classCate/index/page/' + page + '/limit/' + that.limit;
            this.apiGet(url).then((res) => {
                if (res.code != 200 ) {
                    this.handleError(res);
                } else {
                    that.list = res.data.list;
                    that.total_count = res.data.total_count;
                }
            });
        },
        saveEditIncell (value) {   //点击单元格编辑方法
            var data = {
                id: value.id,
                name: value.name,
                type: value.type,
                listorder: value.listorder,
            }
            this.apiPost('admin/classCate/update', data).then((res) => {
                if (res.code != 200) {
                    this.handleError(res);
                } else {
                    this.$Message.success('编辑成功');
                    this.getList(this.page);
                }
            })
        },
        edit(id) {
            this.showEdit = true;
            this.$refs.EditModal.getCateInfo(id);
        },  
        remove(id) {
            this.$Modal.confirm({
                width: "200",
                content: '确认是否删除',
                onOk: () => {
                    this.delete(id);
                }
            });
        },
        delete(id) {
            var url = 'admin/classCate/delete/'; 
            this.apiDelete(url, id).then((res) => {
                if (res.code != 200) {
                    this.$Message.error(res.msg);
                } else {
                    this.getList(this.page);
                    this.$Message.success(res.msg);
                    
                }
            });
        },
        closeAdd () {
            this.showAdd = false;
            this.getList(this.page);
        },
        closeEdit() {
            this.showEdit = false;
            this.getList(this.page);
        }
    },
    created () {
        this.getList(this.page)
    },
  mixins: [http]
}
</script>
